package com.jdone.compus.repository;

import com.jdone.compus.model.StudentWhitelist;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface StudentWhitelistRepository extends JpaRepository<StudentWhitelist, Long> {

    List<StudentWhitelist> findByNameContainingAndRoomContainingAndIsDeletedFalse(String name, String room);

    Optional<StudentWhitelist> findByStudentId(String studentId);

    Page<StudentWhitelist> findByNameLikeAndRoomLike(String name, String room, Pageable pageable);

    @Query("SELECT s FROM StudentWhitelist s WHERE " +
            "s.isDeleted = false AND " +
            "(:name IS NULL OR s.name LIKE %:name%) AND " +
            "(:room IS NULL OR s.room LIKE %:room%)AND " +
            "(:studentId IS null OR s.studentId LIKE %:studentId%)")
    Page<StudentWhitelist> findByNameAndRoom(
            @Param("name") String name,
            @Param("room") String room,
            @Param("studentId" ) String studentId,
            Pageable pageable);
}
